package com.cxhc.demo1006;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.cxhc.demo1006.module.GirlsBean;

import java.util.List;
import java.util.zip.Inflater;

import retrofit2.http.PUT;

public class MainAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public int TYPE_CARD = 0;
    public int TYPE_OTHERS = 1;

    public interface OnItemClickCallback {
        void onItemClick(int pos, GirlsBean girlsBean, ImageView imageView);
    }

    public OnItemClickCallback onItemClickCallback;

    public void setOnItemClickCallback(OnItemClickCallback onItemClickCallback) {
        this.onItemClickCallback = onItemClickCallback;
    }

    private GirlsBean girlsBean;
    private Context context;

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        if (viewType == TYPE_CARD) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_card, parent, false);
            CardViewHolder viewHolder = new CardViewHolder(view);
            return viewHolder;
        } else if (viewType == TYPE_OTHERS) {
            View view = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
            GirlsViewHolder viewHolder = new GirlsViewHolder(view);
            return viewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof CardViewHolder) {

        } else if (holder instanceof GirlsViewHolder) {

                final GirlsViewHolder girlsViewHolder = (GirlsViewHolder) holder;
                String url = girlsBean.getResults().get(position-1).getUrl();
                Glide.with(context).load(url).into(girlsViewHolder.imageView);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onItemClickCallback != null) {
                            onItemClickCallback.onItemClick(position-1, girlsBean, girlsViewHolder.imageView);
                        }
                    }
                });
            }

        }


        @Override
        public int getItemCount () {
            if (girlsBean != null) {
                return girlsBean.getResults().size() + 1;
            } else return 1;
        }

        public void setNewData (GirlsBean girlsBean){
            this.girlsBean = girlsBean;
            notifyDataSetChanged();
        }

        public void addNewData (GirlsBean girlsBean){
//        this.girlsBean = girlsBean;
            int oldSize = 0;
            int newSize = 0;
            if (this.girlsBean == null) {
                this.girlsBean = girlsBean;
                notifyDataSetChanged();
            } else {
                oldSize = this.girlsBean.getResults().size();
                this.girlsBean.getResults().addAll(girlsBean.getResults());
                newSize = this.girlsBean.getResults().size();
//        this.girlsBean.setResults(list);
                notifyDataSetChanged();
//            notifyItemRangeChanged(oldSize,newSize);
            }
        }

        @Override
        public int getItemViewType ( int position){
            if (position == 0) {
                return TYPE_CARD;
            } else {
                return TYPE_OTHERS;
            }
//        return super.getItemViewType(position);
        }


    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            GridLayoutManager gridLayoutManager = (GridLayoutManager) manager;
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    if (position==0){
                        return 6;
                    }else {
                        return 3;
                    }
                }
            });
        }
    }

        public void clear () {
            this.girlsBean = null;
            notifyDataSetChanged();
        }


        class GirlsViewHolder extends RecyclerView.ViewHolder {
            ImageView imageView;

            public GirlsViewHolder(@NonNull View itemView) {
                super(itemView);
                imageView = itemView.findViewById(R.id.iv);
            }
        }

        class CardViewHolder extends RecyclerView.ViewHolder {
//        ImageView imageView;

            public CardViewHolder(@NonNull View itemView) {
                super(itemView);
//            imageView = itemView.findViewById(R.id.iv);
            }
        }
    }
